package com.smartum.mpr.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.smartum.mpr.exception.DatabaseIntegrityException;
import com.smartum.mpr.exception.NotCorrectPermission;
import com.smartum.mpr.exception.NotExistsSexyException;
import com.smartum.mpr.model.Sexy;
import com.smartum.mpr.model.UploadedImageSexy;

@Repository
public class UploadedImageSexyDao extends GenericDao<Long, UploadedImageSexy> {


	public Sexy getSexy(Long userId, Long imageId) throws NotExistsSexyException, DatabaseIntegrityException{
		DetachedCriteria criteria = DetachedCriteria.forClass(UploadedImageSexy.class);
		criteria.createCriteria("user").add(Restrictions.eq("id", userId));
		criteria.createCriteria("image").add(Restrictions.eq("id", imageId));

		List sexys = hibernateTemplate.findByCriteria(criteria);

		if (sexys.size() == 0) {
			throw new NotExistsSexyException();
		} else if (sexys.size() > 1) {
			throw new DatabaseIntegrityException("database has few equals sexys");
		} else if (sexys.size() == 1) {
			return (Sexy) sexys.get(0);
		} else {
			throw new DatabaseIntegrityException();
		}
	}

	@Override
	protected Class<UploadedImageSexy> getEntityClass() {
		return UploadedImageSexy.class;
	}
}
